{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "710fdd93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting langchain_openai\n",
      "  Downloading langchain_openai-0.3.27-py3-none-any.whl (70 kB)\n",
      "     ---------------------------------------- 70.4/70.4 kB 3.8 MB/s eta 0:00:00\n",
      "Requirement already satisfied: openai<2.0.0,>=1.86.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langchain_openai) (1.91.0)\n",
      "Requirement already satisfied: tiktoken<1,>=0.7 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langchain_openai) (0.9.0)\n",
      "Requirement already satisfied: langchain-core<1.0.0,>=0.3.66 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langchain_openai) (0.3.66)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_openai) (9.1.2)\n",
      "Requirement already satisfied: pydantic>=2.7.4 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_openai) (2.11.7)\n",
      "Requirement already satisfied: langsmith>=0.3.45 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_openai) (0.4.2)\n",
      "Requirement already satisfied: PyYAML>=5.3 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_openai) (6.0.2)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_openai) (1.33)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_openai) (24.2)\n",
      "Requirement already satisfied: typing-extensions>=4.7 in c:\\users\\administrator\\appdata\\roaming\\python\\python310\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain_openai) (4.14.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain_openai) (4.9.0)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain_openai) (0.10.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain_openai) (0.28.1)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain_openai) (1.9.0)\n",
      "Requirement already satisfied: sniffio in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain_openai) (1.3.1)\n",
      "Requirement already satisfied: tqdm>4 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain_openai) (4.67.1)\n",
      "Requirement already satisfied: regex>=2022.1.18 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from tiktoken<1,>=0.7->langchain_openai) (2024.11.6)\n",
      "Requirement already satisfied: requests>=2.26.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from tiktoken<1,>=0.7->langchain_openai) (2.32.4)\n",
      "Requirement already satisfied: idna>=2.8 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.86.0->langchain_openai) (3.10)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in c:\\users\\administrator\\appdata\\roaming\\python\\python310\\site-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.86.0->langchain_openai) (1.3.0)\n",
      "Requirement already satisfied: httpcore==1.* in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.86.0->langchain_openai) (1.0.9)\n",
      "Requirement already satisfied: certifi in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.86.0->langchain_openai) (2025.6.15)\n",
      "Requirement already satisfied: h11>=0.16 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.86.0->langchain_openai) (0.16.0)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.66->langchain_openai) (3.0.0)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<1.0.0,>=0.3.66->langchain_openai) (3.10.18)\n",
      "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<1.0.0,>=0.3.66->langchain_openai) (0.23.0)\n",
      "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<1.0.0,>=0.3.66->langchain_openai) (1.0.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<1.0.0,>=0.3.66->langchain_openai) (0.7.0)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<1.0.0,>=0.3.66->langchain_openai) (0.4.1)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<1.0.0,>=0.3.66->langchain_openai) (2.33.2)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain_openai) (3.4.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\administrator\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain_openai) (2.5.0)\n",
      "Requirement already satisfied: colorama in c:\\users\\administrator\\appdata\\roaming\\python\\python310\\site-packages (from tqdm>4->openai<2.0.0,>=1.86.0->langchain_openai) (0.4.6)\n",
      "Installing collected packages: langchain_openai\n",
      "Successfully installed langchain_openai-0.3.27\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip available: 22.2.2 -> 25.1.1\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "pip install langchain_openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bb327378",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_15616\\4108817809.py:15: PydanticDeprecatedSince20: Pydantic V1 style `@validator` validators are deprecated. You should migrate to Pydantic V2 style `@field_validator` validators, see the migration guide for more details. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.11/migration/\n",
      "  @validator(\"setup\")\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'为什么熊猫喜欢吃竹子？'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.llms import OpenAI\n",
    "from langchain.output_parsers import PydanticOutputParser\n",
    "from langchain.prompts import PromptTemplate\n",
    "from pydantic import BaseModel,Field,validator\n",
    "import os\n",
    "api_key=os.getenv(\"OPENAI_API_KEY\")\n",
    "model=OpenAI(\n",
    "    model=\"gpt-3.5-turbo-instruct\",\n",
    "    temperature=0,\n",
    "    openai_api_key=api_key,\n",
    "    )\n",
    "class Joke(BaseModel):\n",
    "    setup:str =Field(description=\"设置笑话问题\")\n",
    "    punchline:str=Field(description=\"回答笑话的答案\")\n",
    "    @validator(\"setup\")\n",
    "    def question_mark(cls, v):\n",
    "        if not (v.endswith(\"?\") or v.endswith(\"？\")):\n",
    "            raise ValueError(\"不符合问题格式，应该以问号结尾\")\n",
    "        return v\n",
    "\n",
    "parser=PydanticOutputParser(pydantic_object=Joke)\n",
    "prompt=PromptTemplate(\n",
    "    template=\"回答用户的输入.\\n{format_instrc}\\n{query}\\n\",\n",
    "    input_variables=[\"query\"],\n",
    "    partial_variables={\"format_instrc\":parser.get_format_instructions()}\n",
    "\n",
    "    )\n",
    "prompt_model=prompt|model|parser\n",
    "output=prompt_model.invoke({\"query\":\"给我讲一个笑话\"})\n",
    "output.setup\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d92aa5b3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
